<?php
//先设置命名空间为后台，控制器
namespace Admin\Controller;
//使用Think文件夹里面的Controller类，并一定要继承controller，才可使自定义控制正常使用

use Think\Controller;
class AdController extends CommonController{

    //定义控制器的方法
    function index(){
        $ad = M('ad');
        $list = $ad->select();
        $this->assign('list',$list);
        $this -> display();
    }

    function create(){
        //添加图片
        import('ORG.Net.UploadFile');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/ad/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录

        // 上传文件
        $info = $upload->upload();

        $filename = $info['image']['savepath'] . $info['image']['savename'];
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {

        }
        $_POST['image'] = $filename;
        $model = D('ad');
        $model->create();
        if ($model->add() > 0) {
            $this->success('创建成功', U('index'));
        } else {
            $this->error('添加失败');
        }
    }

    function delete()
    {
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }

        $ad_id = $_POST['ad_id'];
        if (empty($ad_id)) {
            $this->ajaxReturn(array(
                'info' => '链接不存在'
            ));
        }

        $ad = M('ad');
        $result = $ad->delete($ad_id);
        if ($result > 0) {
            $data = array(
                'info' => 'ok',
            );
        } else {
            $data = array(
                'info' => '删除失败'
            );
        }
        $this->ajaxReturn($data);
    }

}
